package com.thinkgem.jeesite.common.utils;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import org.apache.commons.csv.CSVFormat;
import org.apache.commons.csv.CSVRecord;
import org.apache.commons.lang3.StringUtils;

import java.io.*;
import java.text.DecimalFormat;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2017/5/3.
 */
public class MetricsReader {
    public enum Headers {
    	//Notes,Hours
        //t, count, mean_rate, m1_rate, m5_rate, m15_rate, rate_unit
    }

    private List<File> getFileList(String strPath) {
        File dir = new File(strPath);
        List<File> filelist = Lists.newArrayList();
        File[] files = dir.listFiles(); // 该文件目录下文件全部放入数组
        if (files != null) {
            for (int i = 0; i < files.length; i++) {
                String fileName = files[i].getName();
                if (files[i].isDirectory()) { // 判断是文件还是文件夹
                    filelist.addAll(getFileList(files[i].getAbsolutePath())); // 获取文件绝对路径
                } else if (fileName.endsWith(".csv")) { // 判断文件名是否以.csv结尾
                    filelist.add(files[i]);
                } else {
                    continue;
                }
            }
        }
        return filelist;
    }

    private double getMaxRate(File file) throws IOException {
        Reader in = new FileReader(file);
        Iterable<CSVRecord> records = CSVFormat.RFC4180.withHeader(Headers.class).parse(in);
        double maxValue = 0D;
        for (CSVRecord record : records) {
            if (record.getRecordNumber() == 1) {//跳过第一行，第一行是header
                continue;
            }
//            double rowMaxRate = getRowMaxRate(record);
//            if (rowMaxRate > maxValue) {
//                maxValue = rowMaxRate;
//            }
        }
        return maxValue;
    }

//    private double getRowMaxRate(CSVRecord record) {
//        double m1rate = Double.parseDouble(record.get(Headers.m1_rate));
//        double m5rate = Double.parseDouble(record.get(Headers.m5_rate));
//        double m15rate = Double.parseDouble(record.get(Headers.m15_rate));
//        double maxRate = m1rate;
//        if (m5rate > maxRate) {
//            maxRate = m5rate;
//        }
//        if (m15rate > maxRate) {
//            maxRate = m15rate;
//        }
//        return maxRate;
//    }

    private Map<String, Double> reduce(Map<File, Double> rates) {
        Map<String, Double> moduleRates = Maps.newLinkedHashMap();
        for (Map.Entry<File, Double> entry : rates.entrySet()) {
            String moduleName = getModuleName(entry.getKey().getName());
            Double moduleRate = moduleRates.containsKey(moduleName) ? moduleRates.get(moduleName) : 0D;
            moduleRate += entry.getValue();
            moduleRates.put(moduleName, moduleRate);
        }
        return moduleRates;
    }

    private void write(Map<String, Double> rates, File writeFile) throws IOException {
        if (writeFile.exists()) {
            writeFile.delete();
        }
        BufferedWriter writer = new BufferedWriter(new FileWriter(writeFile));
        StringBuffer header = new StringBuffer();
        header.append("sys").append("\t\t\t").append("tps");
        writer.write(header.toString());
        writer.newLine();
        for (Map.Entry<String, Double> entry : rates.entrySet()) {
            StringBuffer line = new StringBuffer();
            line.append(entry.getKey()).append("\t\t\t").append(new DecimalFormat("######0.00").format(entry.getValue()));
            writer.write(line.toString());
            writer.newLine();
        }
        writer.close();
    }

    private void write2(Map<File, Double> rates, File writeFile) throws IOException {
        if (writeFile.exists()) {
            writeFile.delete();
        }
        BufferedWriter writer = new BufferedWriter(new FileWriter(writeFile));
        StringBuffer header = new StringBuffer();
        header.append("sys").append("\t\t\t").append("tps");
        writer.write(header.toString());
        writer.newLine();
        for (Map.Entry<File, Double> entry : rates.entrySet()) {
            StringBuffer line = new StringBuffer();
            line.append(entry.getKey().getName()).append("\t\t\t").append(new DecimalFormat("######0.00").format(entry.getValue()));
            writer.write(line.toString());
            writer.newLine();
        }
        writer.close();
    }

    private String getModuleName(String filename) {
        if (filename.startsWith("com.winit")) {
            String[] nameParts = StringUtils.split(filename, ".");
            return nameParts[2];
        }
        return "default";
    }

    public static void main(String[] args) throws IOException {
        MetricsReader metricsReader = new MetricsReader();
        String path = "E:/winit/打卡考勤/美国/0523/Ky_lunch05102017.csv";
        
        Reader in = new FileReader(path);
        Iterable<CSVRecord> records = CSVFormat.RFC4180.withHeader(Headers.class).parse(in);
        double maxValue = 0D;
        for (CSVRecord record : records) {
            if (record.getRecordNumber() == 1) {//跳过第一行，第一行是header
                continue;
            }else{
            	System.out.println(record.get("Company Code"));
            	System.out.println(record.get("Employee ID"));
            	System.out.println(record.get("In time"));
            	System.out.println(record.get("Out time"));
            	
            }
        }
        
//        List<File> files = metricsReader.getFileList(path);
//        Map<File, Double> moduleRates = Maps.newLinkedHashMap();
//        for (File file : files) {
//            double rate = metricsReader.getMaxRate(file);
//            moduleRates.put(file, rate);
//        }
//        Map<String, Double> result = metricsReader.reduce(moduleRates);
//        metricsReader.write2(moduleRates, new File(path + "\\result.txt"));
    }
}
